//
// Created by 张祎鹤严 on 2022/12/12.
//

#ifndef __INA226_H
#define __INA226_H

#include "bsp_iic.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os2.h"

#define 	CFG_REG	 		0x00		//
#define 	SV_REG 			0x01		//分流电压
#define 	BV_REG 			0x02		//总线电压
#define 	PWR_REG 		0x03		//电源功率
#define 	CUR_REG 		0x04		//电流
#define 	CAL_REG 		0x05		//校准，设定满量程范围以及电流和功率测数的
#define 	ONFF_REG 		0x06		//屏蔽 使能 警报配置和转换准备就绪
#define 	AL_REG 			0x07		//包含与所选警报功能相比较的限定值
#define 	INA226_GET_ADDR 0XFF		//包含唯一的芯片标识号
#define   	INA226_ADDR1	0x80
//#define   INA226_GETALADDR	0x14

//定义配置数据
#define 	INA226_VAL_LSB	2.5f	//分流电压 LSB 2.5uV
#define     Voltage_LSB		1.25f   //总线电压 LSB 1.25mV
#define     CURRENT_LSB 	1.0f 	//电流LSB 1mA
#define     POWER_LSB       25*CURRENT_LSB
#define     CAL             456     //0.00512/(Current_LSB*R_SHUNT) = 470  //电流偏大改小

typedef struct
{
    float voltageVal;			//mV
    float Shunt_voltage;		//uV
    float Shunt_Current;		//mA
    float Power_Val;				//功率
    float Power;				//功率mW
    uint32_t INA226_id;
}INA226;

extern INA226 INA226_data;

void INA226_SetRegPointer(uint8_t addr,uint8_t reg);
void INA226_SendData(uint8_t addr, uint8_t reg, uint16_t data);

uint16_t INA226_ReadData(uint8_t addr);
void INA226_Get_ID(uint8_t addr);			//获取 id
uint32_t INA226_GetVoltage(uint8_t addr);		//获取总线电压装载值
int32_t INA226_GetShunt_Current(uint8_t addr);	//获取分流电流装载值
uint32_t INA226_GetShuntVoltage(uint8_t addr);	//分流电压装载值
//u16 INA226_Get_Power(u8 addr);		//获取功率装载值，不使用
uint16_t INA226_GET_CAL_REG(uint8_t addr);		//获取校准值
void INA226_Init(void);

/*
void INA226_Init(void);
void GetVoltage(float *Voltage);
void Get_Shunt_voltage(float *Current);
void Get_Shunt_Current(float *Current);
void GetPower(void);
*/

#endif //_TASK_HI226_H


/*
#ifndef __INA226_H
#define __INA226_H
#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx.h"
//#include "stm32f405xx.h"

#define     I2C_USE         I2C2        //定义I2C口
#define 	CFG_REG	 		0x00		//控制寄存器，设置模式，采样时间等
#define 	SV_REG 			0x01		//分流电压
#define 	BV_REG 			0x02		//总线电压
#define 	PWR_REG 		0x03		//电源功率
#define 	CUR_REG 		0x04		//电流
#define 	CAL_REG 		0x05		//校准，设定满量程范围以及电流和功率测数的
#define 	ONFF_REG 		0x06		//屏蔽 使能 警报配置和转换准备就绪
#define 	AL_REG 			0x07		//包含与所选警报功能相比较的限定值
#define 	INA226_GET_ADDR 0XFF		//包含唯一的芯片标识号
#define     INA226_ADDR1	0x80
#define     WRITE_MISSION   1 //写任务
#define     READ_MISSION    2 //读任务
//#define   INA226_GETALADDR	0x14

//定义配置数据
#define 	INA226_VAL_LSB	2.5f	//分流电压 LSB 2.5uV
#define     Voltage_LSB		1.25f   //总线电压 LSB 1.25mV
#define     CURRENT_LSB 	1.25f 	//电流LSB 1.25mA
#define     POWER_LSB       25*CURRENT_LSB
#define     CAL             2048     //0.00512/(Current_LSB*R_SHUNT) = 4096  //校准寄存器的值

typedef struct
{
	float voltageVal;			//mV
	float Shunt_voltage;		//uV
	float Shunt_Current;		//mA
	float Power_Val;				//功率
	float Power;				//功率mW
	uint32_t   ina226_id;
}INA226;

void INA226_ReadData(uint8_t _devAdder);
void INA226_SendData(uint8_t _devAdder,uint8_t _regAdder, uint16_t _num);

void I2C_IT_EVENT_SELECT(void);

void mx_ina226_init(void);
void GetVoltage(float *Voltage);
void Get_Shunt_voltage(float *Current);
void Get_Shunt_Current(float *Current);
void get_power(void);						////获取功率= 总线电压 * 电流

extern INA226 ina226_data;

#ifdef __cplusplus
}
#endif
#endif
*/